<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\TongjiService;
use App\Service\Admin\WhywService;
use Illuminate\Http\Request;

class WhywController0907 extends BaseController
{
    //工时录入

    //列表
    public function index(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['uid'] = $request->post('uid', 0);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $res = WhywService::index($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //详情
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::getOne($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //设置项目
    public function setByItem(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['staff_id'] = $request->post('uid', 0);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        $pas['item_id'] = $request->post('item_id', 0);
        $pas['task_id'] = $request->post('task_id', 0);
        if (!$pas['item_id']) {
            return self::toJson(-2, '项目必须的');
        }
        $res = WhywService::setByItem($id, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //获取工时配置
    public function getConfigWhList(Request $request)
    {
        $uid = $request->post('uid', 0);
        $res = WhywService::getConfigWhList($uid);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

//    //计算
//    public function jisuan(Request $request)
//    {
//        $start = $request->post('start', '');
//        $end = $request->post('end', '');
//        if (!$start || !$end) {
//            return self::toJson(-2, '没有参数');
//        }
//        if ($start > $end) {
//            return self::toJson(-2, '开始时间大于结束时间');
//        }
//        $res = WhywService::getOption();
//        $hours = $res['data']['hour'];
//        $si = 0; $ei = 0;
//        foreach ($hours as $k => $v) {
//            if ($v == $start) $si = $k;
//            if ($v == $end) $ei = $k;
//        }
//        $delay = ($ei - $si) * 10;
//        $shi = floor($delay / 60);
//        $fen = $delay - $shi * 60;
//        $arys = [
//            'fen' => $delay,
//            'str' => $shi ? $shi.'小时'.$fen.'分钟' : $fen.'分钟',
//        ];
//        return self::toJson(0,'', $arys);
//    }

//    //修改
//    public function modify(Request $request)
//    {
//        $pas['role_id'] = $request->post('role_id', 0);
//        $pas['depart_id'] = $request->post('depart_id', 0);
//        $id = $request->post('id', 0);
//        $pas['item_id'] = $request->post('item_id', 0);
//        $pas['task_id'] = $request->post('task_id', 0);
//        $pas['wh_id'] = $request->post('wh_id', 0);
//        $pas['starthour'] = $request->post('start', '');
//        $pas['endhour'] = $request->post('end', '');
//        $pas['delay'] = $request->post('delay', 0);
//        if ($id) {
//            $res = WhywService::modify($id, $pas);
//        } else {
//            $res = WhywService::add($pas);
//        }
//        if ($res['code'] < 0) {
//            return self::toJson(-2, $res['msg']);
//        }
//        return self::toJson();
//    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::del($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //审核列表
    public function authIndex(Request $request)
    {
        $pages['start'] = $request->post('start', 1);
        $pages['limit'] = $request->post('limit', 10);
        $pas['role_id'] = $request->post('role_id', 0);
        $pas['depart_id'] = $request->post('depart_id', 0);
        if (!in_array($pas['role_id'], [1,2,3,5])) {
            return self::toJson(-2, '必须是审核员或者主管');
        }
        $res = WhywService::authIndex($pages, $pas);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }

    //审核通过
    public function authOk(Request $request)
    {
        $id = $request->post('id', 0);
        $res = WhywService::authOk($id);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //审核通过
    public function authNo(Request $request)
    {
        $id = $request->post('id', 0);
        $remark = $request->post('remark', '');
        $res = WhywService::authNo($id, $remark);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson();
    }

    //工时统计
    public function tongji(Request $request)
    {
        $type = $request->post('type', 0);
        $uid = $request->post('uid', 0);
        $res = TongjiService::tongji($type, $uid);
        if ($res['code'] < 0) {
            return self::toJson(-2, $res['msg']);
        }
        return self::toJson(0, '', $res['data']);
    }
}